﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cms.Application.Admin.Views;
using Cms.Application.IO;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class CustomizeEditorPresenter : BasePresenter
    {
        private readonly Administrator _administrator;
        private readonly ICustomizeEditorView _view;

        public IDiskAccess DiskAccess { get; set; }

        public CustomizeEditorPresenter(ICustomizeEditorView view)
            : this(view, new RepositoryFactory())
        {
            DiskAccess = new DiskAccess();
        }

        public CustomizeEditorPresenter(ICustomizeEditorView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
            _administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            if (!_administrator.HasPermission.EditContent)
                _view.RedirectToPermissionDeniedPage();
        }

        public void Initialize()
        {
            _view.CustomJson = DiskAccess.ReadTextFile("Admin\\Resources\\Customer-TinyMCE-JSON-Config.js");
            _view.InitJavascript = DiskAccess.ReadTextFile("Admin\\Resources\\Customer-TinyMCE-Init.js");
        }

        public void Save()
        {
            if (_view.CurrentTab == "customJson")
            {
                DiskAccess.CreateTextFile("Admin\\Resources\\Customer-TinyMCE-JSON-Config.js", _view.CustomJson);
            }
            else
            {
                DiskAccess.CreateTextFile("Admin\\Resources\\Customer-TinyMCE-Init.js", _view.InitJavascript);
            }
        }
    }
}
